package com.such.kit.database.bean;

import java.util.HashMap;
import java.util.Map;
import com.such.kit.database.dialect.TypeFactory;
import com.such.kit.database.exception.DatabaseException;
import com.such.kit.database.support.DDLBean;
import com.such.kit.datacast.CastCommons;
import com.such.kit.string.StringCommons;
import com.such.kit.validate.ValidateCommons;

public class DDLColumn extends DDLBean<DatabaseColumn> {

	private static final long serialVersionUID = 4319598184529103626L;

	private String schema;
	private String tableName;
	private String columnName;
	private String dataType;
	private int columnSize;
	private int decimalDigits;
	private String defaultValue;
	private boolean notNull;
	private boolean autoincrement;
	private String columnComment;

	// 列信息扩展
	private String changedColumnName;

	public DDLColumn() {
		
	}

	public DDLColumn(DatabaseColumn column) {
		converter(column);
	}

	public String getSchema() {
		return this.schema;
	}

	public void setSchema(String schema) {
		this.schema = schema;
	}

	public String getTableName() {
		return this.tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getColumnName() {
		return this.columnName;
	}

	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}

	public String getDataType() {
		return this.dataType;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}

	public int getColumnSize() {
		return this.columnSize;
	}

	public void setColumnSize(int columnSize) {
		this.columnSize = columnSize;
	}

	public int getDecimalDigits() {
		return this.decimalDigits;
	}

	public void setDecimalDigits(int decimalDigits) {
		this.decimalDigits = decimalDigits;
	}

	public String getDefaultValue() {
		return this.defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public boolean getNotNull() {
		return this.notNull;
	}

	public void setNotNull(boolean notNull) {
		this.notNull = notNull;
	}

	public boolean getAutoincrement() {
		return this.autoincrement;
	}

	public void setAutoincrement(boolean autoincrement) {
		this.autoincrement = autoincrement;
	}

	public String getColumnComment() {
		return this.columnComment;
	}

	public void setColumnComment(String columnComment) {
		this.columnComment = columnComment;
	}

	public String getChangedColumnName() {
		return this.changedColumnName;
	}

	public void setChangedColumnName(String changedColumnName) {
		this.changedColumnName = changedColumnName;
	}

	public void converter(DatabaseColumn column) {
		if (ValidateCommons.isNotEmpty(column)) {
			setSchema(column.getSchema());
			setTableName(column.getTableName());
			setColumnName(column.getColumnName());
			setDataType(column.getDataTypeName());
			if (ValidateCommons.isNotEmpty(column.getColumnSize())) {
				setColumnSize(CastCommons.stringToInteger(column.getColumnSize()));
			}
			if (ValidateCommons.isNotEmpty(column.getDecimalDigits())) {
				setDecimalDigits(CastCommons.stringToInteger(column.getDecimalDigits()));
			}
			if (!CastCommons.stringToBoolean(column.getNullable())) {
				setNotNull(true);
			}
			setDefaultValue(column.getDefaultValue());
			if (CastCommons.stringToBoolean(column.getIsAutoincrement())) {
				setAutoincrement(true);
			}
			setColumnComment(column.getComment());
		}
	}

	public Map<String, String> buildRendererParams(Map<String, String> resourceKeyMapping, Map<String, String> ddlTemplateMapping, Map<String, String> params, TypeFactory type) {
		if (ValidateCommons.isEmpty(type)) {
			throw new DatabaseException("未指定 TypeFactory");
		}
		String tableName = this.tableName;
		if (ValidateCommons.isNotEmpty(this.schema) && ValidateCommons.isNotEmpty(tableName)) {
			tableName = StringCommons.merger(this.schema, ".", tableName);
		}
		String dataType = type.typeRenderer(this.dataType, this.columnSize, this.decimalDigits);

		Map<String, String> columnParams = new HashMap<String, String>();
		if (ValidateCommons.isNotEmpty(params)) {
			columnParams.putAll(params);
		}
		putString("tableName", tableName, columnParams, resourceKeyMapping, ddlTemplateMapping);
		putString("columnName", this.columnName, columnParams, resourceKeyMapping, ddlTemplateMapping);
		putString("dataType", dataType, columnParams, resourceKeyMapping, ddlTemplateMapping);
		putBoolean("notNull", this.notNull, columnParams, resourceKeyMapping, ddlTemplateMapping);
		putString("defaultValue", this.defaultValue, columnParams, resourceKeyMapping, ddlTemplateMapping);
		putBoolean("autoincrement", this.autoincrement, columnParams, resourceKeyMapping, ddlTemplateMapping);
		putString("columnComment", this.columnComment, columnParams, resourceKeyMapping, ddlTemplateMapping);
		putString("changedColumnName", this.changedColumnName, columnParams, resourceKeyMapping, ddlTemplateMapping);
		return columnParams;
	}

}
